﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using cpe.cpein.UI.UserCpeInServiceReference;

namespace cpe.cpein.UI.Controls
{
    /// <summary>
    /// Interaction logic for UCNews.xaml
    /// </summary>
    public partial class UCNews : UserControl
    {
        #region Variables declaration

        /// <summary>
        /// Connection to WebService User
        /// </summary>
        UsersServiceClient _userService = new UsersServiceClient();
        
        /// <summary>
        /// Current User.
        /// </summary>
        public User _currentUser { get; set; }

        #endregion

        #region Create and Open

        public UCNews()
        {
            InitializeComponent();
        }

        public UCNews(User currentUser)
        {
            InitializeComponent();
            this._currentUser = currentUser;
        }
        #endregion

        #region Event Methods

        /// <summary>
        /// Method fires when control is Loaded.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            loadMessageList();
        }

        /// <summary>
        /// Method to share a message
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnShare_Click(object sender, RoutedEventArgs e)
        {
            if (this._currentUser != null) { 
            
            Tools.Tools.BeginWaitStatus();
            Message msg = new Message();
            msg.Content = tbxMyNews.Text;
            msg.IsDeleted = false;
            msg.IsPrivate = false;
            msg.Subject = String.Empty;
            msg.IDSender = this._currentUser.Id;
            msg.IDReceiver = this._currentUser.Id; //public message
            msg.SenderDel = false;
            msg.ReceiverDel = false;
            msg.ReceiverRead = true;
            msg.Date = DateTime.Now;

            msg = _userService.AddNews(msg, this._currentUser);
            this.loadMessageList();
            Tools.Tools.EndWaitStatus();
        }
        }

        /// <summary>
        /// Refresh the list
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnRefresh_Click(object sender, RoutedEventArgs e)
        {
            this.loadMessageList();
        }

        #endregion

        #region Methods
        

        /// <summary>
        /// Function used to to initialize the current UserControl.
        /// </summary>
        private void loadMessageList()
        {
            if(_currentUser!=null){
                Tools.Tools.BeginWaitStatus();
                List<Message> messages = (from m in _userService.GetNewsFeed(this._currentUser, this._currentUser)
                                         orderby m.Date descending
                                         select m).ToList();
                MessageGrid.ItemsSource = messages;
                Tools.Tools.EndWaitStatus();
            }
        }
        #endregion

    }
}
